package com.supor.aiot.food.adapter;

import android.content.Context;
import android.graphics.Typeface;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.supor.aiot.common.ui.view.DotTextView;
import com.supor.aiot.food.R;
import com.supor.aiot.food.bean.ChildList;

import java.util.List;


public class RecipeTagsAdapter extends RecyclerView.Adapter<RecipeTagsAdapter.RecipeDeviceHolder>{
    private List<ChildList> mList;
    private Context mContext;
    private onItemClickListener recipeDeviceClick;

    public RecipeTagsAdapter(List<ChildList> mList, Context mContext , onItemClickListener listener) {
        this.mList = mList;
        this.mContext = mContext;
        this.recipeDeviceClick = listener;
    }

    public void initPosition(int position){
        for (int i=0;i<mList.size();i++){
            mList.get(i).setSelected(false);
        }
        mList.get(position).setSelected(true);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecipeTagsAdapter.RecipeDeviceHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(mContext).inflate(R.layout.item_common_tag_title,parent, false);
        return new RecipeTagsAdapter.RecipeDeviceHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecipeDeviceHolder holder, int position) {
        ChildList classifyBean = mList.get(position);
        holder.llSearchOuter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                recipeDeviceClick.OnItemClicked(position);
            }
        });
        if (classifyBean.isSelected()){

            holder.tvSearchContent.setTextColor(mContext.getResources().getColor(R.color.content_default));
            holder.tvSearchContent.setTextSize(TypedValue.COMPLEX_UNIT_PX,mContext.getResources().getDimension(R.dimen.sp_16));
            holder.tvSearchContent.setTypeface(Typeface.DEFAULT_BOLD);
            holder.tvSearchContent.setDotVisible(true);
        }else {
            holder.tvSearchContent.setTextColor(mContext.getResources().getColor(R.color.content_assist));
            holder.tvSearchContent.setTextSize(TypedValue.COMPLEX_UNIT_PX,mContext.getResources().getDimension(R.dimen.sp_13));
            holder.tvSearchContent.setTypeface(Typeface.DEFAULT);
            holder.tvSearchContent.setDotVisible(false);
        }
        holder.tvSearchContent.setText(classifyBean.getClassificationName());
    }


    @Override
    public int getItemCount() {
        return mList.size();
    }

    public class RecipeDeviceHolder extends RecyclerView.ViewHolder{
        public RelativeLayout llSearchOuter;
        public DotTextView tvSearchContent;
        public RecipeDeviceHolder(@NonNull View itemView) {
            super(itemView);
            llSearchOuter=itemView.findViewById(R.id.rl_tag_outer);
            tvSearchContent=itemView.findViewById(R.id.tv_tag_desc);
        }
    }

    public interface onItemClickListener {
        void OnItemClicked(int position);
    }

}
